package com.iss.yunpan.controller;

import com.iss.yunpan.entity.Users;
import com.iss.yunpan.service.IndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@SessionAttributes("users")
@Controller
public class IndexController {

    @Autowired
    private IndexService service;

    @RequestMapping("login")
    public String login(Model model, HttpSession session, String username, String password){
        Users user = service.getUsersByUsernameAndPassword(username,password);
        if(username==null || "".equals(username) || password==null ||"".equals(password)){
            model.addAttribute("msg","用户名或者密码不能为空");
            return "index";
        }
        if(user==null){
            model.addAttribute("msg","用户名或者密码不正确");
            return "index";
        }
        System.out.println(user.getRealName());
        model.addAttribute("users",user);
        session.setAttribute("users", user);
        return "main";
    }
    @RequestMapping("register")
    public String register()
    {
        return "register";
    }


    @RequestMapping("saveUser")
    public String saveUser(Model model,Users users)
    {
        boolean flag=service.insertUser(users);
        if (flag){
            model.addAttribute("users",users);
            return "success";
        }
        model.addAttribute("msg","注册失败，请检查所填内容是否符合规范！");
        //System.out.println(users);
        return "register";
    }

    private void insertUser(Users users) {
    }

    @RequestMapping("checkUserByUserId")
    @ResponseBody
    public boolean checkUserByUserId(String userId){
        if (!"".equals(userId))
        {
            boolean flag=service.checkUserByUserId(userId);
            return  flag;
        }
        return false;
    }
    @RequestMapping("fileAll")
    public  String allFile()
    {
        return "file/fileList.html";
    }

    @RequestMapping("myShare")
    public  String myShare()
    {
        return "file/shareList.html";
    }

    @RequestMapping("logOut")
    public  String logOut(HttpServletRequest request, SessionStatus sessionStatus)
    {
        sessionStatus.setComplete();
        request.getSession().removeAttribute("users");
        return "redirect:/";

    }
}
